package com.solace.selenium.service.impl;

import cn.hutool.cron.CronUtil;
import com.alibaba.fastjson.JSONArray;
import com.solace.selenium.config.ServiceException;
import com.solace.selenium.entity.SysTask;
import com.solace.selenium.entity.TaskData;
import com.solace.selenium.entity.TimeStatus;
import com.solace.selenium.enums.TaskTypeEnum;
import com.solace.selenium.service.TaskService;
import com.solace.selenium.service.WebDriverService;
import com.solace.selenium.task.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    WebDriverService webDriverService;
    @Override
    public String schedule(SysTask sysTask) {
        String cron = sysTask.getCron();
        String goodId = sysTask.getGoodId();
        Assert.hasText(cron,"操作时间不能为空");
        String schedule;
        String ops = sysTask.getOps();
        if (TaskTypeEnum.REMOVE.getOps().equals(ops)){
            //下架
            Assert.hasText(goodId,"操作商品不能为空");
            RemoveGoods task = new RemoveGoods(goodId,webDriverService);
            schedule= CronUtil.schedule(cron, task);
        }else if (TaskTypeEnum.ADD.getOps().equals(ops)){
            //上架
            Assert.hasText(goodId,"操作商品不能为空");
            AddGoods task = new AddGoods(goodId,webDriverService);
            schedule= CronUtil.schedule(cron, task);
        }else if (TaskTypeEnum.COMMENTATOR.getOps().equals(ops)){
            //讲解
            Assert.hasText(goodId,"操作商品不能为空");
            GoodsCommentator task = new GoodsCommentator(goodId,webDriverService);
            schedule= CronUtil.schedule(cron, task);
        }else if (TaskTypeEnum.SETGOODSNUMBER.getOps().equals(ops)){
            //序号
            Assert.hasText(goodId,"操作商品不能为空");
            String number = sysTask.getNumber();
            Assert.hasText(number,"操作商品序号不能为空");
            GoodsNumber task = new GoodsNumber(goodId, number,webDriverService);
            schedule= CronUtil.schedule(cron, task);
        }else if (TaskTypeEnum.GOODSPRIMARY.getOps().equals(ops)){
            //预选
            Assert.hasText(goodId,"操作商品不能为空");
            GoodsPrimary task = new GoodsPrimary(goodId,webDriverService);
            schedule= CronUtil.schedule(cron, task);
        }else if (TaskTypeEnum.ADDGOODSDETERMINE.getOps().equals(ops)){
            //确认
            Assert.hasText(goodId,"操作商品不能为空");
            AddGoodsDetermine task = new AddGoodsDetermine(webDriverService,goodId);
            schedule= CronUtil.schedule(cron, task);
        }else if (TaskTypeEnum.NOCOMMENTATOR.getOps().equals(ops)){
            //取消讲解
            Assert.hasText(goodId,"操作商品不能为空");
            GoodsNotCommentator task = new GoodsNotCommentator(goodId,webDriverService);
            schedule= CronUtil.schedule(cron, task);
        }
        else {
            throw new ServiceException("未知操作类型!");
        }
        sysTask.setScheduleId(schedule);
        TaskData.map.add(sysTask);
        return schedule;
    }

    @Override
    public void start() {
        // 支持秒级别定时任务
        CronUtil.setMatchSecond(true);
        CronUtil.start();
        TimeStatus.flag = true;
    }

    @Override
    public void addAndStart(List<SysTask> sysTasks) {
        if (TimeStatus.flag ){
            stop();
        }
        for (SysTask sysTask : sysTasks) {
            schedule(sysTask);
        }
        start();
    }

    @Override
    public void stop() {
        if (TimeStatus.flag){
            CronUtil.stop();
        }
        TimeStatus.flag = false;
        TaskData.map.clear();
    }

    @Override
    public List<SysTask> getTaskAll() {
        return  TaskData.map;
    }

    @Override
    public void batchUpdate(String data) {
        Assert.hasText(data,"数据不能为空");
        List<SysTask> sysTasks = JSONArray.parseArray(data, SysTask.class);
        for (SysTask sysTask : sysTasks) {
            schedule(sysTask);
        }
    }
}
